﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Xml;
using System.Xml.Serialization;

namespace RecursosHumanos
{
    public partial class frmLocacao : Form
    {
        WBGEntities bd = new WBGEntities();

        public frmLocacao()
        {
            InitializeComponent();
        }

        private void frmFilme_Load(object sender, EventArgs e)
        {

            AtualizaGrid();
        }

        private void AtualizaGrid()
        {
            grd.DataSource = from x in bd.Locacao
                             select new { x.idLocacao, x.Cliente.Pessoa.nome, x.data, x.pagoNaRetirada };//bd.Filme;
            FormataGrid();
        }

        private void FormataGrid()
        {
            grd.Columns[0].HeaderText = "ID";
            grd.Columns[1].HeaderText = "Cliente";
            grd.Columns[2].HeaderText = "Data";
            grd.Columns[3].HeaderText = "Pago";
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void grd_Click(object sender, EventArgs e)
        {
            try
            {
                //grd.CurrentRow.Cells[0].ToolTipText = FilmeGenero.Find(Convert.ToInt32(grd.CurrentRow.Cells["genero"].Value.ToString()));
            }
            catch { }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            frmLocacao_ frm = null;
            try
            {
                frm = new frmLocacao_(Request.Inserir);
                frm.ShowDialog();
                AtualizaGrid();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.InnerException.Message, "Erro ao inserir", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (frm != null)
                    frm.Dispose();
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            try
            {
                int id = Convert.ToInt32(grd.CurrentRow.Cells["idLocacao"].Value);
                var frm = new frmLocacao_(Request.Alterar);
                frm.locacao = bd.Locacao.FirstOrDefault(x => x.idLocacao == id);
                frm.ShowDialog();
                AtualizaGrid();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.InnerException.Message, "Erro ao alterar", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("Deseja realmente excluir?", "Confirmação", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
                    return;

                int id = Convert.ToInt32(grd.CurrentRow.Cells["idLocacao"].Value);
                var reg = bd.Locacao.FirstOrDefault(x => x.idLocacao == id);
                bd.DeleteObject(reg);
                bd.SaveChanges();
                AtualizaGrid();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.InnerException.Message, "Erro ao excluir", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
